//
//  weatherBtn.m
//  Cunli
//
//  Created by lilubin on 15/7/15.
//  Copyright (c) 2015年 lilubin. All rights reserved.
//

#import "weatherBtn.h"
#import "MyDataService.h"
#import "Config.h"
#import "NSString+MD5.h"
#import "UIButton+WebCache.h"
#import "UIView+UIViewController.h"
#import "WeatherInfoVC.h"
//#import "weatherManager.h"
#import "User.h"

static weatherBtn *shareSelf = nil;


@interface weatherBtn()
{
    NSString *_imgURL;
    NSString *_weatherTitle;
    NSDictionary *_weatherResult;
    NSString *_temperatureTitle;
}

@end

@implementation weatherBtn


- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        [self loadWeatherData];
        /*
         weatherManager *btnWeatherManager = [weatherManager sharedManager];
         
         if ((btnWeatherManager.imgURL.length != 0) && (btnWeatherManager.weatherTitle.length != 0)) {
         [self setViewKitWithImgUrl:btnWeatherManager.imgURL btnTitle:btnWeatherManager.weatherTitle];
         }
         else
         {
         btnWeatherManager.setBtnUIKitts = ^(NSString *imgUrl, NSString *temperature){
         [self setViewKitWithImgUrl:imgUrl btnTitle:temperature];
         };
         }
         */
        
    }
    return self;
}


- (void)loadWeatherData
{
    __weak typeof(self) weakSelf = self;
    NSMutableDictionary *weatherDic = [NSMutableDictionary dictionaryWithCapacity:2];
    /*
    [weatherDic setObject:[Config getOwnUserID] forKey:@"userid"];
    [weatherDic setObject:[Config getOwnAreaids] forKey:@"areaids"];
    NSString *resultSign = [NSString getSignFromParam:weatherDic andSession:[Config getOwnSession]];
     */
    [weatherDic setObject:[Login curLoginBaseUser].userid forKey:@"userid"];
    [weatherDic setObject:[Login curLoginBaseUser].areaids forKey:@"areaids"];
    NSString *resultSign = [NSString getSignFromParam:weatherDic andSession:[Login curLoginBaseUser].usersession];
    [weatherDic setObject:resultSign forKey:@"sign"];
    
    [MyDataService requestURL:@"weather.php" httpMethod:@"GET" params:weatherDic fileDatas:nil complection:^(id result) {
        NSString *response = result[@"status"];
        _weatherResult = result;
        NSLog(@">>>weather result>>>:%@", _weatherResult);
        if ([response isEqualToString:@"success"]) {
            NSMutableArray *resultsArray = [NSMutableArray array];
            resultsArray = result[@"results"];
            NSMutableDictionary *weatherResults = [NSMutableDictionary dictionary];
            weatherResults = resultsArray[0];
            NSArray *weatherDataArray = weatherResults[@"weather_data"];
            _imgURL = [weatherDataArray[0] objectForKey:@"dayPictureUrl"];
            _temperatureTitle = [weatherDataArray[0] objectForKey:@"temperature"];
            _weatherTitle = [weatherDataArray[0] objectForKey:@"weather"];
            [weakSelf setViewKit];
        }
        else if([response isEqualToString:@"error"])
        {
            
        }
    } failureAction:^{
    }];
    
}

- (void)setViewKit
{
    /*
    UIButton *imgBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    imgBtn.frame = CGRectMake(0, 0, 40, 40);
    [imgBtn setImageWithURL:[NSURL URLWithString:_imgURL] placeholderImage:[UIImage imageNamed:@"default_image"]];
    [imgBtn addTarget:self action:@selector(pushToWeather) forControlEvents:UIControlEventTouchUpInside];
    imgBtn.layer.cornerRadius = 12;
    imgBtn.layer.masksToBounds = YES;
    [self addSubview:imgBtn];
    */
    
     UILabel *weatherLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 18, self.frame.size.width-20, 21)];
    weatherLabel.textAlignment = NSTextAlignmentRight;
    
     [weatherLabel setFont:[UIFont systemFontOfSize:13.0f]];
     [weatherLabel setTextColor:[UIColor whiteColor]];;
     weatherLabel.text = _weatherTitle;
     [self addSubview:weatherLabel];
    
    UILabel *temperatureLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width-20, 21)];
    temperatureLabel.textAlignment = NSTextAlignmentRight;
    [temperatureLabel setFont:[UIFont systemFontOfSize:13.0f]];
    [temperatureLabel setTextColor:[UIColor whiteColor]];
    [temperatureLabel setText:_temperatureTitle];
    [self addSubview:temperatureLabel];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(pushToWeather)];
    tap.numberOfTouchesRequired = 1;
    tap.numberOfTapsRequired = 1;
    [self addGestureRecognizer:tap];
}

/*
- (void)setViewKitWithImgUrl:(NSString *)btnImgUrl btnTitle:(NSString *)title
{
    UIButton *imgBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    imgBtn.frame = CGRectMake(0, 0, 40, 40);
    [imgBtn setImageWithURL:[NSURL URLWithString:btnImgUrl] placeholderImage:[UIImage imageNamed:@"default_image"]];
    [imgBtn addTarget:self action:@selector(pushToWeather) forControlEvents:UIControlEventTouchUpInside];
    imgBtn.layer.cornerRadius = 12;
    imgBtn.layer.masksToBounds = YES;
    [self addSubview:imgBtn];
 
    UILabel *weatherLabel = [[UILabel alloc] initWithFrame:CGRectMake(42, 10, 62, 30)];
    [weatherLabel setFont:[UIFont systemFontOfSize:12]];
    [weatherLabel setTextColor:[UIColor whiteColor]];;
    weatherLabel.text = title;
    [self addSubview:weatherLabel];
}
*/

- (void)pushToWeather
{
    /*
    if (_weatherResult.count != 0) {
        WeatherInfoVC *weatherVC = [[WeatherInfoVC alloc] init];
        weatherVC.weatherDic = _weatherResult;
        [self.viewController presentViewController:weatherVC animated:YES completion:nil];
    }
     */
    NSLog(@"PUSH TO WEATHER");
    self.buttonClicked(_weatherResult);
}


@end
